# -*- coding: utf-8 -*-

# Copyright 2011 Google Inc. All Rights Reserved.

# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at

#     http://www.apache.org/licenses/LICENSE-2.0

# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

from google.appengine.dist import use_library
use_library('django', '1.2')

import datetime
import hashlib
import logging
import os
import random
from google.appengine.api import users
from google.appengine.ext import db
from google.appengine.ext.webapp import template
from google.appengine.ext.db import djangoforms

from django import forms

def produce_hash(name, email, now=datetime.datetime.now()):
    h = hashlib.sha1()
    h.update(name.encode('utf-8'))
    h.update(email.encode('utf-8'))
    h.update(now.ctime())
    return h.hexdigest()

class Event(db.Model):
    name = db.StringProperty(required=True)
    place_name = db.StringProperty(required=True)
    start_ts = db.DateTimeProperty(required=True)
    end_ts = db.DateTimeProperty(required=True)
    address = db.PostalAddressProperty(required=True)
    add_ts = db.DateTimeProperty(auto_now_add=True)
    add_user = db.UserProperty(auto_current_user_add=True)    

class Guest(db.Model):
    name = db.StringProperty(required=True, verbose_name='Nome')
    email = db.EmailProperty(required=True, verbose_name='E-mail')
    add_ts = db.DateTimeProperty(auto_now_add=True)
    add_user = db.UserProperty(auto_current_user_add=True)
    mail_ts = db.DateTimeProperty()
    read_ts = db.DateTimeProperty()
    rsvp_ts = db.DateTimeProperty()
    rsvp = db.StringProperty(default='No', required=True, 
                             choices=['No', 'Yes', 'Maybe'],
                             verbose_name='Você vai a festa ?')
    extra_guests = db.IntegerProperty(default=0, verbose_name=(
            'Numero de convidados extras (considere somente convidados '
            'com mais de 4 anos)'))
    extra_names = db.TextProperty(verbose_name=(
            'Nome dos convidados (liste o nome de todos)'))

class GuestForm(djangoforms.ModelForm):
    
    name = forms.CharField(max_length=80, label='Nome')
    email = forms.CharField(max_length=80, label='E-mail')
    rsvp = forms.CharField(widget=forms.Select(
            choices=[('No', 'Não'), ('Yes', 'Sim'), 
                     ('Maybe', 'Talvez')]), label='Você vai a festa?')
    # extra_names = forms.TextField(label='Convidados adicionais')

    class Meta:
        model = Guest
        exclude = ['mail_ts', 'read_ts', 'rsvp_ts']


# Set the main event and your e-mail here, this will be the ancestor
# for your party at this time.
name = u'Farewell party to switzerland'
email = u'raphael@menderico.com.br'
add_ts = datetime.datetime(2011, 5, 22, 0, 0, 0)
main_event = Event.get_or_insert(
    key_name=produce_hash(name, email, add_ts),
    name=name, place_name = u'Buffet Happy Hunters',
    start_ts = datetime.datetime(2011, 7, 30, 19, 0, 0),
    end_ts = datetime.datetime(2011, 7, 30, 23, 0, 0),
    address = (u'Rua General Osório, 2230, Cambui, Campinas, SP, Brazil')) 
